package com.zkzx.freight.bill.controller;

import com.zkzx.common.exception.BaseException;
import com.zkzx.common.freight.command.OrderCommand;
import com.zkzx.common.freight.model.OrderListVO;
import com.zkzx.common.freight.model.OrderVO;
import com.zkzx.common.freight.param.OrderFilterParam;
import com.zkzx.common.freight.vo.OrderStatisticsVO;
import com.zkzx.common.vo.PageResult;
import com.zkzx.common.vo.RequestFilterParams;
import com.zkzx.common.vo.StatusVO;
import com.zkzx.freight.bill.service.OrderService;
import com.zkzx.util.json.JSONResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 北京中科智兴物联科技有限公司 @版权所有
 * @Author: zhaoyihe@zkraising.com
 * @Date: 2021/05/12/17:52
 * @Description:
 */
@RestController
@Validated
@Api( tags="订单管理")
@RequestMapping(value = "/orders")
public class OrderController extends BaseController {

    @Autowired
    private OrderService orderService;

    @GetMapping
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "获取订单列表", notes = "获取订单列表")
    public PageResult<OrderListVO> find(@ApiParam(value = "查询参数") @RequestParam("filterParam") String filterParamJson,
                                        @ApiParam(value = "分页查询参数")  @RequestParam("params") String paramsJson)  {

        OrderFilterParam filterParam = (OrderFilterParam) JSONResultUtil.toObject(filterParamJson, OrderFilterParam.class);
        RequestFilterParams params = (RequestFilterParams) JSONResultUtil.toObject(paramsJson, RequestFilterParams.class);

        return orderService.find(filterParam,params);

    }

    @GetMapping("/{id}")
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "获取订单详细", notes = "获取订单详细")
    public OrderVO getById(@PathVariable("id") Integer id) {
        //司机收款信息，电话，留到API接口去做
        return orderService.getById(id);
    }

    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    @ApiOperation(value = "新增运单信息", notes = "新增运单信息")
    public StatusVO add(@RequestBody @Valid OrderCommand command) throws BaseException {
        return orderService.add(command,super.getUserId());
    }

    @PutMapping("/{id}")
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "修改运单信息", notes = "修改运单信息")
    public void update(@PathVariable("id") Integer id,@RequestBody @Valid OrderCommand command) {
        orderService.update(command,id,super.getUserId());
    }

    @GetMapping("/statistics")
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "获取车辆数量信息", notes = "获取车辆数量信息")
    public OrderStatisticsVO getOrderStatistics(@RequestParam("company_id")Integer companyId)  {
        return orderService.getOrderStatistics( companyId);
    }


    @PostMapping("/{id}/pay")
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "支付", notes = "支付")
    public void payment(@PathVariable("id") Integer id) {
        //支付
    }

    @PostMapping("/{id}/invoice")
    @ResponseStatus(HttpStatus.OK)
    @ApiOperation(value = "申请开票", notes = "申请开票")
    public void invoice(@PathVariable("id") Integer id){
        //申请开票
    }


}
